<template>
    <van-address-edit 
        show-delete
        show-set-default
        :area-list="areaList"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        :address-info="address"
        @save="save"
        @delete="del"
    />
</template>

<script setup>
    //引入地区数据
    import { areaList } from '@vant/area-data';

    const { proxy } = getCurrentInstance();

    // 获取收获地址的编辑ID
    const id = ref(proxy.$route.query.id ? proxy.$route.query.id : 0);

    const address = ref();

    const business = proxy.$business;

    onBeforeMount(() => GetInfo());

    // 查询收获地址方法
    const GetInfo = async () =>
    {
        // 发送请求
        let result = await proxy.$POST({
            url: '/address/info',
            params: {
                busid: business.value.id,
                id:id.value
            }
        })

        if(result.code === 1)
        {
            let data = result.data

            // 修改地址的默认信息
            address.value = {
                name: data.consignee,
                tel: data.mobile,
                addressDetail: data.address,
                areaCode: data.district || data.city || data.province,
                isDefault: data.status == 1 ? true : false
            }

            return false;
        }else
        {
            proxy.$fail({
                message: result.msg,
                onClose: () =>
                {
                    proxy.$router.back();
                }
            })

            return false;
        }
    }

    // 更新
    const save = async (values) =>
    {
        // 组装数据
        let params = {
            busid: business.value.id,
            id: id.value,
            consignee: values.name,
            address: values.addressDetail,
            mobile: values.tel,
            status: values.isDefault ? 1 : 0,
            code: values.areaCode
        }

        // 发送请求
        let result = await proxy.$POST({
            url: "/address/edit",
            params
        })

        if(result.code === 1)
        {
            proxy.$success({
                message: result.msg,
                onClose: () =>
                {
                    proxy.$router.back();

                    return false;
                }
            })
        }else
        {
            proxy.$fail(result.msg);

            return false;
        }
    }

    // 删除
    const del = async (item) => 
    {
        proxy.$confirm({
            title: '删除收货地址',
            message: '是否确认删除该收获地址？'
        }).then(async () => {
            let result = await proxy.$POST({
                url: '/address/del',
                params: {
                    busid: business.value.id,
                    id: id.value
                }
            })

            if(result.code === 1)
            {
                //返回上一个界面
                proxy.$success({
                    message: result.msg,
                    onClose: () =>
                    {
                        proxy.$router.back();

                        return false;
                    }
                })
            }else
            {
                proxy.$fail(result.msg);

                return false;
            }

        }).catch(() => {})
    }
</script>